﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using Microsoft.Web.Administration;
using ServerManager.Models;

namespace ServerManager
{

    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "SiteWCFService" in both code and config file together.
    public class SiteWCFService : ISiteWCFService
    {
        public void DoWork()
        {
            
        }
        public long CreateIISSite(string siteName, string physicalPath, string bindingProtocol, string host, string ip, string port, string frameVersion, bool isIntegrated, bool enable32Bit)
        {
            using (Microsoft.Web.Administration.ServerManager serverManager = new Microsoft.Web.Administration.ServerManager())
            {
                string bindInformation = string.Format("{0}:{1}:{2}", ip, port, host);
                serverManager.Sites.Add(siteName,bindingProtocol,bindInformation,physicalPath);

                serverManager.CommitChanges();

                //程序池操作
                DelApplicationPool(siteName);
                AddApplicationPool(siteName,frameVersion, isIntegrated, enable32Bit);
                serverManager.Sites[siteName].ApplicationDefaults.ApplicationPoolName = siteName;

                serverManager.CommitChanges();
                return serverManager.Sites[siteName].Id;
            }
        }



        public List<string> GetSites()
        {
            List<Site> list = new List<Site>();
            List<string> strings = new List<string>();
            using (Microsoft.Web.Administration.ServerManager serverManager = new Microsoft.Web.Administration.ServerManager())
            {
#if DEBUG
                Console.WriteLine(serverManager.Sites.Count);
#endif
                foreach (Site site in serverManager.Sites)
                {
                    list.Add(site);
                    strings.Add(site.Name);
                }
            }
            return strings;

        }
        #region 方法
        bool DelApplicationPool(string poolName) {
            try
            {
                using (Microsoft.Web.Administration.ServerManager serverManager = new Microsoft.Web.Administration.ServerManager()) {
                    ApplicationPool appPool = serverManager.ApplicationPools[poolName];
                    if (appPool!=null)
                    {
                        serverManager.ApplicationPools.Remove(appPool);
                        serverManager.CommitChanges();
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
             //   return false;
                throw ex;
            }
        }
        bool AddApplicationPool(string poolName, string frameVer, bool isIntegrated, bool enable32Bit) {
            try
            {
                using (Microsoft.Web.Administration.ServerManager serverManager = new Microsoft.Web.Administration.ServerManager())
                {
                    ApplicationPool pool = serverManager.ApplicationPools.Add(poolName);
                    pool.Enable32BitAppOnWin64 = enable32Bit;
                    pool.AutoStart = true;
                    pool.ManagedPipelineMode = isIntegrated ? ManagedPipelineMode.Integrated : ManagedPipelineMode.Classic;
                    pool.ManagedRuntimeVersion = frameVer;
                    pool.ProcessModel.IdentityType = ProcessModelIdentityType.ApplicationPoolIdentity;
                    serverManager.CommitChanges();
                }
                return true;
            }
            catch (Exception ex)
            {
                
                throw;
            }
        }
        #endregion

        public void CreateFTPSUUsers(string loginid, string password, string dir, long max)
        {
            using (Models.suEntities dbcontext=new Models.suEntities())
            {
                SUUsers user = new SUUsers()
                {
                    LoginID = loginid,
                    Password = password,
                    HomeDir = dir,
                    Quota = max.ToString()
                };
                dbcontext.SUUsers.Add(user);
                dbcontext.SaveChanges();
                
            }
        }

        public void ResetApplicationPool(string ApplicationPoolName, string frameVersion, bool isIntegrated, bool enable32Bit)
        {
            using (Microsoft.Web.Administration.ServerManager serverManager = new Microsoft.Web.Administration.ServerManager())
            {
                ApplicationPool pool = serverManager.ApplicationPools[ApplicationPoolName];
                pool.Enable32BitAppOnWin64 = enable32Bit;
                pool.AutoStart = true;
                pool.ManagedPipelineMode = isIntegrated ? ManagedPipelineMode.Integrated : ManagedPipelineMode.Classic;
                pool.ManagedRuntimeVersion = frameVersion;
                pool.ProcessModel.IdentityType = ProcessModelIdentityType.ApplicationPoolIdentity;
                serverManager.CommitChanges();
            }
        }


        public void RestartApplicationPool(string ApplicationPoolName)
        {
            using (Microsoft.Web.Administration.ServerManager serverManager = new Microsoft.Web.Administration.ServerManager())
            {
                ApplicationPool pool = serverManager.ApplicationPools[ApplicationPoolName];
                pool.Stop();
                pool.Start();
                serverManager.CommitChanges();
            }
        }
    }
}
